
//用户选择完成后组装asidebar数据存入后台（选择的全部的叶子节点， 选择了一半的父节点）
export function customAsideData(tmpData, leafNode) {
    let dataList = [];
    // 保存叶子节点为全选中的父节点
    tmpData.forEach(item => {
        if (item.hasOwnProperty("children")) {
            dataList.push(item)
        }
    })

    leafNode.forEach(val => {
        let children = val.children
        val.children = []
        children.forEach(value => {
            tmpData.forEach(item => {
                if (!item.hasOwnProperty("children")) {
                    if (value.label === item.label) {
                        val.children.push(item)
                    }
                }
            })
        })
        dataList.push(val)
    })
    // 根据id重新进行排序
    dataList.sort((item1,item2)=>{
        return item1.id - item2.id
    })
    let treeData = [...dataList]

    // 封装为对象
    let tmpObj = {};
    treeData.forEach((item) => {
        if (item.hasOwnProperty("children")) {
            let itemObj = {};
            item.children.forEach((value) => {
                itemObj[value.id] = value.label;
            });
            tmpObj[item.label] = itemObj;
        }
    });
    let tempRoleAside = {}
    tempRoleAside["AsideBar"] = tmpObj;
    return tempRoleAside;
}

//从后台请求的数据组装成el-tree数据格式

export function customTreeData(listData) {
    let asideList = [];
    let tmpData = eval("(" + listData + ")");
    let list = tmpData.AsideBar;
    let indexArr = [];
    Object.keys(list).forEach((key, index) => {
        let childrenObj = {};
        let itemObj = {};
        itemObj["id"] = index;
        itemObj["label"] = key;
        itemObj["children"] = [];
        let itemValue = list[key];
        for (let ckey in itemValue) {
          let childrenItem = {};
          childrenItem["id"] = ckey;
          childrenItem["label"] = itemValue[ckey];
            itemObj["children"].push(childrenItem);
            indexArr.push(ckey);
        }
        // console.log(index);
        // this.listChoose.push(index);
    asideList.push(itemObj);
    });
    return asideList;
}

